import uniqueId from '@form-create/utils/lib/unique';
import {makeOptionsRule} from "@/components/formDesigner/utils";

const label = '表单数据加载';
const name = 'loadDataAuto';

export default {
    icon: 'icon-divider',
    label,
    name,
    rule() {
        return {
            type: name,
            field: name+'1',
            props:{
                maxHeight: 400,
                minHeight: 400,
                total:0,
                url: '/taskflow/coding/dynamicTaskflow/executeTask',
                method: 'post',
                paramData:{"taskFlowId": 9, "route":"selectById"},
                data: []
            },
            on:{
                /*loadData:({fApi, componentId, param})=>{
                    console.log('loadDataAuto.loadData()')
                    console.log(param)
                    // param
                    const vm = fApi.el(componentId)
                    const formCreateInject = vm.formCreateInject
                    let url = formCreateInject.prop.props.url
                    url=url?url.trim():''
                    let method = formCreateInject.prop.props.method
                    method=method?method.trim():''
                    let paramData = formCreateInject.prop.props.paramData
                    paramData=paramData?paramData.trim():'{}'
                    const paramDataJson = JSON.parse(paramData)
                    Object.assign(param, paramDataJson)
                    // api
                    const request = ()=>require('@/utils/request')
                    request().default({
                        url,
                        method,
                        data: param
                    }).then(r => {
                        console.log(r)
                        if (!r.success) {
                            return;
                        }
                        const dataList = r.obj
                        console.log('dataList')
                        console.log(dataList)
                        if (dataList.length==0) {
                            return;
                        }
                        const data = dataList[0]
                        fApi.setValue(data)
                    })
                }*/
            },
        };
    },
    props() {
        return [
            {
                type: 'input',
                field: 'url',
                title: '请求URL',
                props: {
                    type: 'textarea',
                    rows: 4,
                },
            },
            {
                type: 'input',
                field: 'method',
                title: 'method(get,post)',
            },
            {
                type: 'Struct',
                field: 'paramData',
                value: [],
                info: '自动加入页面调用请求参数',
                title: '追加请求参数',
                props: {
                    defaultValue: {},
                    validate(val) {
                        return true
                        /*if (!Array.isArray(val)) return false;
                        if (!val.length) return true;
                        return !val.some(({rule}) => {
                            return !Array.isArray(rule);
                        });*/
                    }
                }
            },
        ];
    }
};
